<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['museums:question:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['museums:question:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['museums:question:remove']"
        >删除</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题目编号" align="center" prop="id" width="160" />
      <el-table-column label="标题" align="center" prop="title" width="320" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Qrcode" @click="show_handleQr_code(scope.row)">二维码</el-button>
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['museums:question:edit']"
          >修改</el-button>
          <!-- <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['museums:question:remove']"
          >删除</el-button> -->
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            v-hasPermi="['museums:question:query']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 二维码框 -->
    <el-dialog :title="title" v-model="qrcode" width="600px" align="center" append-to-body>
      <el-form :model="form" :rules="rules" label-width="120px">
        <div id="qrcode-fix"></div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel3">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加或修改简历信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="questionRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="问题描述" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="5" placeholder="问题描述"></el-input>
          <!-- <wang-editor v-model="form.description" /> -->
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <!-- <el-input v-model="form.image" type="textarea" placeholder="图片"></el-input> -->
          <wang-editor v-model="form.image" />
        </el-form-item>
        <el-form-item label="是否多选">
          <el-switch
            v-model="form.status"
            inactive-value="0"
            active-value="1"
            inline-prompt
            active-text="多选"
            inactive-text="单选"
          />
        </el-form-item>
        <el-row>
          <el-col :span="16">
            <el-form-item label="选项A" prop="option1">
              <el-input v-model="form.option1" placeholder="请输入选项A" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-switch
                v-model="optionA"
                inline-prompt
                active-text="是"
                inactive-text="否"
                @change="switchChange(1)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="选项B" prop="option2">
              <el-input v-model="form.option2" placeholder="请输入选项B" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-switch
                v-model="optionB"
                inline-prompt
                active-text="是"
                inactive-text="否"
                @change="switchChange(2)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="选项C" prop="option3">
              <el-input v-model="form.option3" placeholder="请输入选项C" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-switch
                v-model="optionC"
                inline-prompt
                active-text="是"
                inactive-text="否"
                @change="switchChange(3)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="选项D" prop="option4">
              <el-input v-model="form.option4" placeholder="请输入选项D" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-switch
                v-model="optionD"
                inline-prompt
                active-text="是"
                inactive-text="否"
                @change="switchChange(4)"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel1">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 题目详细 -->
    <el-dialog title="题目详细" v-model="detail" width="700px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题:">{{ form.title }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="问题描述:">{{ form.description }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选项A:">{{ form.option1 }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选项B:">{{ form.option2 }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选项C:">{{ form.option3 }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选项D:">{{ form.option4 }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="答案:">{{ form.answer }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel2">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Question">
import {
  listQuestion,
  getQuestion,
  delQuestion,
  addQuestion,
  updateQuestion
} from "@/api/museums/question";
import { ref, toRaw } from "vue";
import QRCode from "qrcodejs2-fix";
import { createApp } from "vue";
import wangEditor from "@/views/tool/editor/wangEditor.vue";

const { proxy } = getCurrentInstance();
const questionList = ref([]);
const open = ref(false);
const detail = ref(false);
const qrcode = ref(false);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const judg = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    description: null,
    image: null,
    title: null,
    option1: null,
    option2: null,
    option3: null,
    option4: null
  },
  rules: {
    title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
    description: [
      { required: true, message: "问题描述不能为空", trigger: "blur" }
    ],
    option1: [{ required: true, message: "选项A不能为空", trigger: "blur" }],
    option2: [{ required: true, message: "选项B不能为空", trigger: "blur" }],
    option3: [{ required: true, message: "选项C不能为空", trigger: "blur" }],
    option4: [{ required: false, message: "选项D可以为空", trigger: "blur" }],
    answer: [{ required: true, message: "答案不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询题目列表 */
function getList() {
  loading.value = true;
  listQuestion(queryParams.value).then(response => {
    questionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 新增和修改取消按钮
function cancel1() {
  open.value = false;
}

// 详细取消按钮
function cancel2() {
  detail.value = false;
}
// 二维码取消按钮
function cancel3() {
  qrcode.value = false;
}
/** 详细按钮操作 */
function handleView(row) {
  detail.value = true;
  form.value = row;
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    description: " ",
    image: " ",
    status: 0,
    optionA: " ",
    optionB: " ",
    optionC: " ",
    optionD: " ",
    option1: null,
    option2: null,
    option3: null,
    option4: null,
    answer: null,
    create_time: null,
    update_time: null
  };
  proxy.resetForm("questionRef");
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加题目";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  status.value = false;
  optionA.value = false;
  optionB.value = false;
  optionC.value = false;
  optionD.value = false;
  const _id = row.id || ids.value;
  getQuestion(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改题目";
  });
}
/**二维码按钮操作 */
function show_handleQr_code(row) {
  qrcode.value = true;
  title.value = "二维码";
  const _id = row.id || ids.value;
  nextTick(() => {
    document.getElementById("qrcode-fix").innerHTML = "";

    let url =
      "http://192.168.137.1:80/web/qid/" +
      JSON.parse(JSON.stringify(questionList.value))[_id - 1].id;
    new QRCode(document.getElementById("qrcode-fix"), {
      text: url
    });
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["questionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        form.value.answer = answer.value;
        console.log(form.value.answer);
        updateQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        form.value.answer = answer.value;
        addQuestion(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          console.log(optionD.value);
          open.value = false;
          getList();
        });
      }
    }
  });
}

// /** 删除按钮操作 */
// function handleDelete(row) {
//   const _ids = row.id || ids.value;
//   proxy.$modal
//     .confirm('是否确认删除题目编号为"' + _ids + '"的数据项？')
//     .then(function() {
//       return delQuestion(_ids);
//     })
//     .then(() => {
//       getList();
//       proxy.$modal.msgSuccess("删除成功");
//     })
//     .catch(() => {});
// }
/** 下载按钮操作 */

getList();

//开关控制按钮

const status = ref(0);
const optionA = ref(false);
const optionB = ref(false);
const optionC = ref(false);
const optionD = ref(false);
const answer = ref("");
//添加事件
const switchChange = option => {
  let selfValue = "";
  // status = ref(0);
  // optionA = ref(false);
  // optionB = ref(false);
  // optionC = ref(false);
  // optionD = ref(false);
  // answer = ref("");
  if (status.value === 0) {
    if (option === 1) {
      //vue判断运算符 === 1为true;==='1'为false
      selfValue = "A";
      optionB.value = false;
      optionC.value = false;
      optionD.value = false;
      status.value = "0";
    } else if (option === 2) {
      selfValue = "B";
      optionA.value = false;
      optionC.value = false;
      optionD.value = false;
      status.value = "0";
    } else if (option === 3) {
      selfValue = "C";
      optionA.value = false;
      optionB.value = false;
      optionD.value = false;
      status.value = "0";
    } else if (option === 4) {
      selfValue = "D";
      optionA.value = false;
      optionB.value = false;
      optionC.value = false;
      status.value = "0";
    }
  } else {
    optionA.value === true ? (selfValue += "A") : (selfValue += "");
    optionB.value === true ? (selfValue += "B") : (selfValue += "");
    optionC.value === true ? (selfValue += "C") : (selfValue += "");
    optionD.value === true ? (selfValue += "D") : (selfValue += "");
    status.value = "1";
  }
  answer.value = selfValue;
};
</script>